package com.fannuo.demo.Api;


import com.fannuo.demo.Util.hadoop.HadoopClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
@Component
@Slf4j
public class HadoopApi {
    private HadoopClient hadoopClient;

    public HadoopApi( @Value("${hadoop.host}") String host, @Value("${hadoop.port}") int port) {
        hadoopClient = new HadoopClient(host,port);
    }

    public HadoopClient getClient() {
        return hadoopClient;
    }

    public void putFiles(String dir) {
        log.info("开始导入文件到hadoop中 " + dir);
        File file = new File(dir);
        if(file.isDirectory()) {
            File[] listFile = file.listFiles();
            if (listFile != null) {
                for (File eachFile : listFile) {
                    if (eachFile.getName().endsWith(".pdf")) {
                        try {
                            hadoopClient.putFile("/meta/" + eachFile.getName(), eachFile);
                            log.info("导入完成 " + eachFile.getName());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        log.info("导入文件夹完成 " + dir);
    }

    public byte[] getFile(String id) {
        try {
            return hadoopClient.getFileToBytes("/meta/"+id+".pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
